#pragma once
// 手柄设备

#include "rtdev/rtdev.h"
#include <string.h>
#include <map>
#include "rtdev/bp_common.h"

namespace rtdev {

const uint16_t BP_CONTROL_FRQ = 125;

struct ButtonEvent
{
    uint32_t trigger_count;
    uint32_t count;
    bool is_trigger;
    bool is_last;  // 在响应按键事件消失后，是否持续
};

class BPControlHandle : public RtDev
{
public:
    static const uint8_t ZUC_BP_SIGNAL_NONE_PRESD;
    static const uint8_t SYS_BP_SIGNAL_POWER_PRESD;
    static const uint8_t ZUC_BP_SIGNAL_POWER_ROBOT_PRESD;
    static const uint8_t ZUC_BP_SIGNAL_RETURN_PRESD;
    static const uint8_t ZUC_BP_SIGNAL_RUN_PAUSE_PRESD;
    static const uint8_t ZUC_BP_SIGNAL_TERMINATE_PRESD;
    static const uint8_t ZUC_BP_SIGNAL_LOCK_PRESD;
    static const uint8_t ZUC_BP_SIGNAL_EN_ROBOT_PRESD;

    static const uint32_t ZUC_BP_SHORT_PRESS_TIME;

    BPControlHandle();
    virtual ~BPControlHandle();
    virtual void update();
    int boot_init();
    int comm_read_button_status(uint8_t data);
    void comm_read_beep_status(uint8_t data) { handle_status_.bp_beep = data == 1 ? true : false; }
    void comm_read_lock_color(uint8_t data) { handle_status_.bp_rgb_lock_color = (ControlHandleColor)data; };
    void comm_read_lock_brightness(uint8_t data) { handle_status_.bp_rgb_lock_brightness = data; };
    void comm_read_logo_color(uint8_t data) { handle_status_.bp_rgb_logol_color = (ControlHandleColor)data; };
    void comm_read_logo_brightness(uint8_t data) { handle_status_.bp_rgb_logol_brightness = data; };
    void comm_read_heartbeat(uint8_t data) { handle_status_.heartbeat = data; };
    void comm_write_beep_status(uint8_t* data) { *data = handle_cmd_.bp_beep; }
    void comm_write_lock_color(uint8_t* data) { *data = handle_cmd_.bp_rgb_lock_color; }
    void comm_write_lock_brightness(uint8_t* data) { *data = handle_cmd_.bp_rgb_lock_brightness; }
    void comm_write_logo_color(uint8_t* data) { *data = handle_cmd_.bp_rgb_logol_color; }
    void comm_write_logo_brightness(uint8_t* data) { *data = handle_cmd_.bp_rgb_logol_brightness; }
    void comm_write_heartbeat(uint8_t* data) { *data = handle_cmd_.heartbeat; }

    void set_logo_color(ControlHandleColor color) { handle_cmd_.bp_rgb_logol_color = color; }
    void set_logo_brightness(uint8_t data) { handle_cmd_.bp_rgb_logol_brightness = data; }
    void set_lock_color(ControlHandleColor color) { handle_cmd_.bp_rgb_lock_color = color; }
    void set_lock_brightness(uint8_t data) { handle_cmd_.bp_rgb_lock_brightness = data; }
    void set_beep(uint8_t data) { handle_cmd_.bp_beep = data; }
    void set_bp_in_init_pos(bool done) { rtrn_init_pos_done_ = done; }

    bool is_beeping() const { return handle_status_.bp_beep; }
    ControlHandleColor lock_color() const { return handle_status_.bp_rgb_lock_color; }
    uint8_t lock_brightness() const { return handle_status_.bp_rgb_lock_brightness; }
    ControlHandleColor logo_color() const { return handle_status_.bp_rgb_logol_color; }
    uint8_t logo_brightness() const { return handle_status_.bp_rgb_logol_brightness; }
    uint8_t heatbeat() const { return handle_status_.heartbeat; }

    ControlHanldeStatus handle_status() const { return handle_status_; }
    BPHandleButton handle_button_event_status();

    void update_bp_cmd();

private:
    void comm_jkcan_map();
    void comm_jkcan_tx_map(int busid);

private:
    std::map<uint8_t, ButtonEvent> button_event_;

    ControlHanldeStatus handle_status_;  // 手柄整体状态
    ControlHanldeStatus handle_cmd_;     // 手柄控制指令

    BPHandleButton button_event_status_;  // 手柄按键事件状态
    uint8_t curr_button_state_ = 0;       // 当前按键按下标志
    bool rtrn_init_pos_done_ = false;     // 机器人回到原点
    int bp_beep_cnt_ = 0;

    void reset_control_button_status(int id);
    void beep_when_press();
    void update_lock_light();
};

// 手柄设备
BPControlHandle* get_bp_handle();

}  // namespace rtdev